package edu.npu.cv.service.impl;

import edu.npu.cv.constant.ResultCode;
import edu.npu.cv.dao.UserDAO;
import edu.npu.cv.entity.ResultDO;
import edu.npu.cv.entity.UserDO;
import edu.npu.cv.service.UserService;
import edu.npu.cv.utils.CommonUtils;
import edu.npu.cv.utils.SHA256Util;
import edu.npu.cv.vo.UserVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName UserService
 * @Description TODO
 * @Author Quentin_zyj
 * @Date 2021/10/5 18:22
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDAO userDAO;

    public ResultDO<UserDO> signIn(UserVO userVO) {

        String account = userVO.getAccount();
        String password = SHA256Util.SHA256(userVO.getPassword());
        // 邮箱登录
        UserDO userDO = null;
        if (CommonUtils.isEmailAddress(account)) {
            userDO = userDAO.getUserDOByEmailAndPassword(account, password);
        } else {
            // 账号登录
            userDO = userDAO.getUserDOByAccountAndPassword(account, password);
        }
        if (userDO == null) {
            // 登录失败
            return new ResultDO<UserDO>(false, ResultCode.ADMIN_NOT_EXIST, ResultCode.MSG_ADMIN_NOT_EXIST, null);
        }
        return new ResultDO<UserDO>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, userDO);
    }

}
